package com.hd.trans.ui.activity;

import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.hd.trans.R;
import com.hd.trans.TranslationInterior;
import com.hd.trans.ui.base.BaseActivity;
import com.hd.trans.utils.Constant;
import com.hd.trans.utils.TextSizeType;
import com.hd.trans.utils.TrackerForTrans;

public class FullScreenActivity extends BaseActivity implements View.OnClickListener {

    private boolean isRotated = false;
    private int mWidth, mHeight;
    private TextView textView;
    private LinearLayout rlView, ll_top, ll_end;
    private ScrollView scroll_view;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_fullscreen;
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
        //埋点
        TranslationInterior.getTrackerListener().view("文本翻译_全屏");
        super.initViewsAndEvents(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);
        textView = findViewById(R.id.my_text_view);
        rlView = findViewById(R.id.rl_view);
        scroll_view = findViewById(R.id.scroll_view);
        ll_top = findViewById(R.id.ll_top);
        ll_end = findViewById(R.id.ll_end);
        String content = getIntent().getStringExtra("content");

        scroll_view.postDelayed(()->{
            textView.setMinHeight(scroll_view.getWidth());
            textView.setText(content);
            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, Constant.textSizeType.getValue());
        }, 200);

        findViewById(R.id.closeView).setOnClickListener(this);
        findViewById(R.id.revolveView).setOnClickListener(this);
        findViewById(R.id.sizeView).setOnClickListener(this);
        findViewById(R.id.closeView2).setOnClickListener(this);
        findViewById(R.id.revolveView2).setOnClickListener(this);
        findViewById(R.id.sizeView2).setOnClickListener(this);
    }

    private void changeTextSize() {
        if(Constant.textSizeType == TextSizeType.Smaller){
            Constant.textSizeType = TextSizeType.Bigger;
        }else if(Constant.textSizeType == TextSizeType.Bigger){
            Constant.textSizeType = TextSizeType.Larger;
        }else if(Constant.textSizeType == TextSizeType.Larger){
            Constant.textSizeType = TextSizeType.Smaller;
        }
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, Constant.textSizeType.getValue());
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if(id == R.id.closeView || id == R.id.closeView2){
            finish();
        }else if(id == R.id.revolveView || id == R.id.revolveView2){
            if(mWidth <= 0 || mHeight <= 0){
                mWidth = rlView.getWidth();
                mHeight = rlView.getHeight();
            }
            FrameLayout.LayoutParams mParams = (FrameLayout.LayoutParams) rlView.getLayoutParams();
            if(isRotated){
                ll_top.setVisibility(View.VISIBLE);
                ll_end.setVisibility(View.GONE);
                mParams.width = mWidth;
                mParams.height = mHeight;
                mParams.leftMargin = 0;
                mParams.topMargin = 0;
                rlView.setLayoutParams(mParams);
                rlView.invalidate();
                rlView.setRotation(0);
            }else {
                ll_top.setVisibility(View.GONE);
                ll_end.setVisibility(View.VISIBLE);
                mParams.width = mHeight;
                mParams.height = mWidth;
                mParams.leftMargin = -(mHeight-mWidth)/2;
                mParams.topMargin = (mHeight-mWidth)/2;
                rlView.setLayoutParams(mParams);
                rlView.invalidate();
                rlView.setRotation(-90);
            }
            isRotated = !isRotated;

            //埋点
            TranslationInterior.getTrackerListener().click(TrackerForTrans.WBFY, "页面旋转");
        }else if(id == R.id.sizeView || id == R.id.sizeView2){
            changeTextSize();

            //埋点
            TranslationInterior.getTrackerListener().click(TrackerForTrans.WBFY, "字体缩放");
        }
    }
}
